package org.liveboardgames.agricola.control.delegates.action.processor
{
	import mx.collections.ArrayCollection;
	import mx.collections.ListCollectionView;
	
	import org.liveboardgames.agricola.control.delegates.helper.GameHelper;
	import org.liveboardgames.agricola.control.delegates.mock.ActionFactory;
	import org.liveboardgames.agricola.control.events.intern.ActionEvent;
	import org.liveboardgames.agricola.model.AgricolaModelLocator;
	import org.liveboardgames.agricola.model.enums.RoundOffsetTypeEnum;
	import org.liveboardgames.agricola.vo.Game;
	import org.liveboardgames.agricola.vo.action.Action;
	import org.liveboardgames.agricola.vo.action.ResourcesOnBoardAction;
	import org.liveboardgames.agricola.vo.player.Player;

	public class TakeResourceFromBoardActionProcessor extends ActionProcessor
	{
		public function TakeResourceFromBoardActionProcessor(action:Action)
		{
			super(action);
		}

		override public function execute():void
		{
			var player:Player = action.target as Player;

			if (action is ResourcesOnBoardAction)
			{
				var resourceAction:ResourcesOnBoardAction = ResourcesOnBoardAction(action);

				var resourceType:String = resourceAction.resourceType;
				var roundNumbers:ListCollectionView = resourceAction.rounds;
				var quantity:int = resourceAction.quantity;
				var roundOffsetType:String = resourceAction.offsetType;

				// Now we need to get the resources at the right rounds
				var game:Game = AgricolaModelLocator.getInstance().game;

				// Compute the number of the rounds where to put the resource
				var realRoundNumbers:ListCollectionView = computeRealRoundNumbers(game, roundNumbers, roundOffsetType);

				// We need to get the exact round numbers when the player gets the resource, and 
				// attach them to the action
				resourceAction.rounds = realRoundNumbers;
				resourceAction.offsetType = RoundOffsetTypeEnum.ROUND_NUMBER;

				// Get the current round
				var currentRound:int = GameHelper.getCurrentRoundNumber(game);

				// If the current round is in the list of rounds when the player should get a
				// resource, give it to them
				var index:int = realRoundNumbers.getItemIndex(currentRound);

				if (index != -1)
				{
					player.farm.addResource(resourceType, quantity);
					realRoundNumbers.removeItemAt(index);
				}

			}

			this.dispatchEvent(new ActionEvent(ActionEvent.ACTION_COMPLETE));
		}

		private function computeRealRoundNumbers(game:Game, roundNumbers:ListCollectionView, offsetType:String):ListCollectionView
		{
			var realRoundNumbers:ListCollectionView = new ArrayCollection();
			// If the round number are absolute, nothing to do
			if (offsetType == RoundOffsetTypeEnum.ROUND_NUMBER)
			{
				realRoundNumbers = roundNumbers;
			}
			// Else we need to get the absolute value of these rounds
			else if (offsetType == RoundOffsetTypeEnum.ADD)
			{
				var currentRound:int = GameHelper.getCurrentRoundNumber(game);
				for each (var roundNumber:int in roundNumbers)
				{
					var newRoundNumber:int = roundNumber + currentRound;
					if (newRoundNumber <= GameHelper.NUMBER_OF_TURNS)
					{
						// The -1 is because the first time the action is called, 
						// we are already one round late
						realRoundNumbers.addItem(newRoundNumber - 1);
					}
				}
			}
			return realRoundNumbers;
		}
	}
}